<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @package PyroCMS
 * @subpackage Blog_SAEF module
 * @category Module
 * @author Rick Holmes
 * @version 1.0    
 * 
 * Admin Controller for Blog_SAEF 
 */  
class Admin extends Admin_Controller {
	
	protected $section = 'items';
		
	private $is_allowed;		
	private $saef_validation_rules;
	private $saef_settings;
	
	/**
	 * The constructor
	 * 	 
	 * @param void
	 * @return void
	 */	 	 	 	
	function __construct()
	{
		parent::__construct();
		
		$this->load->library('form_validation');
		$this->load->language('saef');
		$this->load->model('saef_m');
		
		// set validation rules, needed for stickiness but also user needs to choose one or cancel
		$this->saef_validation_rules = array(
			array(
				'field'	=> 'saef_category',
				'label'	=> 'Category',
				'rules'	=> 'required'
			),
			array(
				'field' => 'saef_editable_wrap',
				'label' => 'Single post wrapper class',
				'rules' => 'required|xss_clean'
			)
		);
				
		// check settings page to see if form enabled
		$this->is_enabled = (bool) $this->saef_m->is_enabled();
		
		// get all the settings
		$this->saef_settings = $this->saef_m->get_saef_settings();
		
		// load with helper for pyro < 2.1 or Assets library for 2.1+
		if (function_exists('css'))
		{
			$this->template->append_metadata(css('saef.css', 'saef'));
		}
		else
		{
			$this->template->append_css('module::saef.css');
		}
	}
	
	/**
	 * Index page, lists categories to choose from
	 * 
	 * @access public	 	 
	 * @param void
	 * @return void
	 * @uses blog/blog_categories_m model
	 */	 	 	 	 	
	public function index()
	{
		$this->form_validation->set_rules($this->saef_validation_rules);
		
		// all blog categories
		$categories = $this->saef_m->get_categories();
		
		// all currently allowed categories
		$allowed_cats = $this->saef_m->get_allowed_category_ids();
		
		// add the "set" to categories
		if (is_array($categories))
		{
			foreach($categories as $cat)
			{
				if (!empty($allowed_cats) && is_array($allowed_cats))
				{
					$cat->set = (in_array($cat->id, $allowed_cats)) ? TRUE : FALSE;
				}
				else
				{
					$cat->set = FALSE;
				}
			} 
		}
		else
		{
			$categories = FALSE;
		}
		
		// all user groups
		$groups = $this->ion_auth_model->get_groups();
		
		// all currently allowed groups
		$allowed_groups = $this->saef_settings->allowed_user_groups;
	
		foreach($groups as $key => $group)
		{
			if ($group->id == 1) // skip admins
			{
				unset($groups[$key]);
			}		
		}
		$groups = array_values($groups);
		
		// add the "set"
		if (is_array($groups))
		{
			foreach($groups as $group)
			{
				if (!empty($allowed_groups) && is_array($allowed_groups)) // tho admns are always there
				{
					$group->set = (in_array($group->id, $allowed_groups)) ? TRUE : FALSE; 
				}
				else
				{
					$group->set = FALSE;
				}
			}
		}
						
		// data needed for form
		$data = array(
			'groups' => $groups,
			'must_moderate' => (bool) $this->saef_settings->require_moderation,
			'categories' => $categories, // all blog categories			
			'enabled' => $this->is_enabled,
			'editable' => (bool) $this->saef_settings->allow_editing,
			'admin_edit' => (bool) $this->saef_settings->admin_edit,
			'editable_wrap' => $this->saef_settings->editable_div,
		);		
		
		if ($this->input->post('btnAction'))
		{
			if ($this->form_validation->run())
			{
				$categories = $this->input->post('saef_category');
				
				$settings = array(
					'require_moderation' => $this->input->post('saef_moderate'),
					'user_groups' => $this->input->post('saef_group'),
					'allow_editing' => $this->input->post('saef_editable'),
					'admin_edit' => $this->input->post('saef_admin_edit'),
					'editable_div' => $this->input->post('saef_editable_wrap')
				);
				
				if ($this->saef_m->set_allowed_categories($this->input->post('saef_category')) && $this->saef_m->update_settings($settings))
				{
					$this->session->set_flashdata('success', lang('saef:success'));
					redirect('saef/admin');
				}
				else
				{
					$this->session->set_flashdata('error', lang('saef:error'));
					redirect('saef/admin');
				}
				
			}
			else
			{
				// the only error is that no category was set, but there may have initially been some set
				// so to avoid leaving unwanted categories in the db, go ahead and delete them all then throw error
				$this->saef_m->delete_allowed_categories();
				
				$this->session->set_flashdata('error', lang('saef:error_choose_category'));
				$this->template->build('admin/configuration', $data);
			}
		}
		else
		{
			$this->template->build('admin/configuration', $data);
		}	
	}
	 	 	 	 	 	 	 	 	
}

/* end of file admin.php */
/* location /addons/shared_addons/modules/saef/controllers/admin.php */